package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.data.CaiPinInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class CaiPinAdapter extends BaseAdapter {

    private Context mContext;

    private List<CaiPinInfo> mList = new ArrayList<CaiPinInfo>();
    boolean caipin_check;

    private List<String> selectCaipin = new ArrayList<String>();

    public CaiPinAdapter(Context context,boolean check) {
        mContext = context;
        caipin_check = check;
    }

    public void updateData(List<CaiPinInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public List<String> getSelectCaipin(){
        return selectCaipin;
    }

    public void clearCaipin(){
        selectCaipin.clear();
    }

    public void setSelectCaipin(List<String> caipin){
        selectCaipin = caipin;
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.caipin_item,null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.caiName = (TextView) convertView.findViewById(R.id.caipin_name);
            childHolder.caiMoney = (TextView) convertView.findViewById(R.id.caipin_money);
            childHolder.checkBox = (CheckBox) convertView.findViewById(R.id.caipin_check);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final CaiPinInfo  fansInfo = mList.get(position);
        if(fansInfo != null){
            String headImgUrl = fansInfo.getDishLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ImageCache imageCache = ImageCache.getInstance(mContext);
                ViewUtil.Size size = ViewUtil.getHeadImageSize(mContext);
                Bitmap cacheBitmap = imageCache.getBitmapFromMemoryCache(imageCache.getCacheKey(headImgUrl,size));
                if (cacheBitmap != null){
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.headImg.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg,headImgUrl, size);
                }
            }else{
                childHolder.headImg.setImageResource(R.drawable.zhanweitu);
            }

            if(fansInfo.getDishName()!=null){
                childHolder.caiName.setText(fansInfo.getDishName());
            }
            if(fansInfo.getPrice()!=null){
                childHolder.caiMoney.setText("￥"+fansInfo.getPrice());
            }

            if (caipin_check){

                childHolder.checkBox.setChecked(false);
                if (containsValue(fansInfo.getDishName())){
                    childHolder.checkBox.setChecked(true);
                }

                childHolder.checkBox.setVisibility(View.VISIBLE);
            }else {
                childHolder.checkBox.setVisibility(View.GONE);
            }

            childHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        selectCaipin.add(fansInfo.getDishName());
                    }else {
                        selectCaipin.remove(fansInfo.getDishName());
                    }
                }
            });


        }

        return convertView;
    }

    private boolean containsValue(String s){
        for (int i=0;i<selectCaipin.size();i++){
            if (selectCaipin.get(i).equals(s)){
                return true;
            }
        }
        return false;
    }

    class ChildHolder{
        ImageView headImg;
        TextView caiName;
        TextView caiMoney;
        CheckBox checkBox;

    }
}
